package com.huawei.appgallery.foundation.ui.css.adapter.param;

import android.util.Log;
import android.view.View;
import com.huawei.appgallery.foundation.ui.css.adapter.param.Parameter.AsyncGenerator;
import com.huawei.appgallery.foundation.ui.css.adapter.param.Parameter.Generator;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSValue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;

public class MethodSignature {
    private static final String TAG = "MethodSignature";
    private Generator generator;
    private Method method;

    public MethodSignature(Method method, Generator generator) {
        this.method = method;
        this.generator = generator;
    }

    public void setGenerator(Generator generator) {
        this.generator = generator;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public final void invoke(View view, CSSValue cSSValue) {
        Object obj = this.generator.get(view, cSSValue);
        if (this.generator instanceof AsyncGenerator) {
            final WeakReference weakReference = new WeakReference(view);
            ((AsyncGenerator) this.generator).call(obj, new GeneratorCallBack() {
                public void done(Object obj) {
                    View view = (View) weakReference.get();
                    if (view != null) {
                        MethodSignature.this.invoke(view, obj);
                    }
                }
            });
            return;
        }
        invoke(view, obj);
    }

    protected void invoke(View view, Object obj) {
        if (obj != null) {
            try {
                if (obj.getClass().isArray()) {
                    this.method.invoke(view, (Object[]) obj);
                    return;
                }
            } catch (RuntimeException e) {
                Log.e(TAG, "invoke failed:" + e.getMessage());
                return;
            } catch (Throwable e2) {
                Log.e(TAG, "invoke failed:", e2);
                return;
            }
        }
        this.method.invoke(view, new Object[]{obj});
    }
}
